<?php

class ExtendedController extends Controller {

    protected $viewValues;
    protected $user;

    function __construct() {
        parent::__construct();

        if(isset($this->session->userdata['logged_in']) && $this->session->userdata['logged_in']) {
            $this->user = new MUser($this->session->userdata['user']['id']);
        }

        spl_autoload_register('self::autoload');
    }

    function loadViews($views, $data) {
        foreach ($views as $view)
        {
            $this->load->view($view, $data[$view]);
        }
    }

    static function autoload($class) {

        $class = strtolower($class);

        if (!strstr($class, 'ci_') && !strstr($class, 'extended')) {

            @include_once(APPPATH . '/libraries/' . $class . EXT);
        }
    }

    /*
     * Experimentell:
     * Installiert einen Controller, falls dieser noch nicht existiert!
     * Keine Ahnung ob diese Funktion benötigt wird.
     */
    static function installController($controller = null, $name = '') {
        if (!is_null($controller)) {
            $controller = strtolower($controller);
            $controllerObject = new MController();
            $controllerObject->get_by_call($controller);
            if(!$controllerObject->exists()) {
                $controllerObject->call = $controller;
                $controllerObject->name = (!empty($name)) ? $name : $controller;
                $controllerObject->save();
            }
            return $controllerObject->id;
        } else {
            return false;
        }
    }

    static function updateController($controller = null, $name = '') {
        if (!is_null($controller)) {
            $controller = strtolower($controller);
            $controllerObject = new MController();
            $controllerObject->get_by_call($controller);
            if($controllerObject->exists()) {
                $controllerObject->call = $controller;
                $controllerObject->name = (!empty($name)) ? $name : $controller;
                $controllerObject->save();
                return true;
            }
        }
        return false;
    }
}